package org.jprogger.myworld.api.response.handler;

import java.util.ArrayList;
import java.util.List;

import org.jprogger.myworld.api.ApiServiceInvocationException;
import org.jprogger.myworld.api.app.ApiParams;
import org.jprogger.myworld.api.model.PhotoAlbum;
import org.jprogger.myworld.api.request.ApiRequest;
import org.jprogger.myworld.api.response.ApiResponse;
import org.jprogger.myworld.api.response.AbstractApiResponseHandler;
import org.jprogger.myworld.api.response.ApiResponseData;
import org.jprogger.myworld.api.response.ApiResponseError;
import org.jprogger.myworld.api.response.DefaultApiResponse;
import org.jprogger.myworld.api.response.DefaultApiResponseData;
import org.jprogger.myworld.api.response.ParseResponseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetListPhotoAlbumApiResponseHandler extends AbstractApiResponseHandler<List<PhotoAlbum>> {

	@Override
	public ApiResponse<List<PhotoAlbum>> handleResponse(ApiRequest request, 	String response) throws ApiServiceInvocationException {
		
		ApiResponseError apiError = null;
		ApiResponseData<List<PhotoAlbum>> apiData = null;
		ApiResponse<List<PhotoAlbum>> apiResponse = new DefaultApiResponse<List<PhotoAlbum>>();
		String format = request.getRequestFormat();
		try 
		{
			apiError = validateResponse(response, format);
			if (apiError == null)
			{
				if (format.equals(ApiParams.JSON_REQUEST_FORMAT))
				{
					apiData = new DefaultApiResponseData<List<PhotoAlbum>>();
					List<PhotoAlbum> albums = new ArrayList<PhotoAlbum>();
					JSONArray jsonPhotoAlbums = new JSONArray(response);
					for (int i = 0; i < jsonPhotoAlbums.length(); i++) 
					{	
						PhotoAlbum album = new PhotoAlbum();
						JSONObject jsonPhotoAlbum = jsonPhotoAlbums.getJSONObject(i);
						album.setId(jsonPhotoAlbum.getString("aid"));
						album.setCoverId(jsonPhotoAlbum.getString("cover_pid"));
						album.setOwnerId(jsonPhotoAlbum.getString("owner"));
						album.setTitle(jsonPhotoAlbum.getString("title"));
						album.setDescription(jsonPhotoAlbum.getString("description"));
						album.setCreatedTime(jsonPhotoAlbum.getLong("created"));
						album.setUpdatedTime(jsonPhotoAlbum.getLong("updated"));
						album.setUpdatedTime(jsonPhotoAlbum.getLong("updated"));
						album.setSize(jsonPhotoAlbum.getLong("size"));
						album.setPrivacy(jsonPhotoAlbum.getInt("privacy"));
						album.setUrl(jsonPhotoAlbum.getString("link"));
						albums.add(album);
					}
					
				}
				else if (format.equals(ApiParams.XML_REQUEST_FORMAT))  
				{
					// TODO: implement XML response format 
					throw new ApiServiceInvocationException("Unsupported response format");
				}
			}
			apiResponse.setError(apiError);
			apiResponse.setResponseData(apiData);
		} 
		catch (JSONException e) 
		{
			throw new ApiServiceInvocationException("Bad response format", e);
		}
		catch (ParseResponseException e) 
		{
			throw new ApiServiceInvocationException("Unable to parse response", e);
		}
		return apiResponse;
	}
}
